<template>
  <div class="save-db-container">
    <el-form 
        :model="formData" 
        label-position="right" 
        label-width="120px"
        :rules="rules"
        ref="formRef"
      >
        <el-form-item label="组态名称" prop="tName">
          <el-input
            v-model="tName"
            placeholder="请输入组态图名称"
            clearable
          />
        </el-form-item>
        
        <el-form-item label="消息主题" prop="tTopic">
          <el-input
            v-model="tTopic"
            placeholder="请输入消息主题"
            clearable
          />
        </el-form-item>
        
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="页面宽度">
              <el-input
                :value="width"
                disabled
              >
                <template #suffix>px</template>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="页面高度">
              <el-input
                :value="height"
                disabled
              >
                <template #suffix>px</template>
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
        
        <el-form-item label="背景颜色">
          <div class="color-display">
            <div 
              class="color-block" 
              :style="{ backgroundColor: tBackgroundColor }"
            ></div>
            <span class="color-value">{{ tBackgroundColor }}</span>
          </div>
        </el-form-item>
        
        <el-form-item label="组件数量">
          <el-tag type="success">{{ svg_data?.length || 0 }}</el-tag>
          <span class="component-hint">个组件</span>
        </el-form-item>
      </el-form>
      
      <div class="button-group">
        <el-button 
          type="primary" 
          size="large"
          @click="submit"
        >
          {{ tId == "" ? "保存" : "修改" }}
        </el-button>
        <el-button 
          size="large"
          @click="emit('closePageBack')"
        >
          取消
        </el-button>
      </div>
  </div>
</template>

<script setup lang="ts">
import { ref, computed } from "vue";
import { ISvgDataLists } from "../../Model";
import { saveTopo, getList } from "@/api/business";
import { ElMessage } from "element-plus";

//调用后端成功后的回调父组件
const emit = defineEmits(["closePageBack"]);
const formRef = ref(null);

const props = defineProps({
  //要渲染的数据
  svg_data: {
    type: Array as () => Array<ISvgDataLists>,
    required: true,
    default: [],
  },
  tId: {
    type: String,
    default: "",
  },
  tName: {
    type: String,
    default: "",
  },
  tTopic: {
    type: String,
    default: "",
  },
  width: {
    type: Number,
  },
  height: {
    type: Number,
  },
  tBackgroundColor: {
    type: String,
    default: "#000000",
  },
});
const formData = ref({
  tName: props.tName,
  tTopic: props.tTopic,
});

// 使用计算属性或直接绑定到 formData
const tName = computed({
  get: () => formData.value.tName,
  set: (val) => { formData.value.tName = val; }
});

const tTopic = computed({
  get: () => formData.value.tTopic,
  set: (val) => { formData.value.tTopic = val; }
});

const rules = {
  tName: [
    { required: true, message: "请输入组态名称", trigger: "blur" },
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  tTopic: [
    { required: true, message: "请输入消息主题", trigger: "blur" },
    { min: 1, max: 100, message: "长度在 1 到 100 个字符", trigger: "blur" },
  ],
};

const submit = async () => {
  if (!formRef.value) return;
  
  // 先验证表单
  const valid = await (formRef.value as any).validate().catch(() => false);
  if (!valid) {
    ElMessage.error("请填写完整信息");
    return;
  }
  
  console.log("data", props.svg_data);
  const json = JSON.stringify(props.svg_data);
  let jsonData = ref(json);

  let data = {
    tId: props.tId,
    tName: formData.value.tName,
    tTopic: formData.value.tTopic,
    tWidth: props.width,
    tHeight: props.height,
    tJsondata: jsonData.value,
    tBackgroundColor: props.tBackgroundColor,
  };
  
  try {
    const res = await saveTopo(data);
    if (res.code == 200) {
      ElMessage.success("保存成功!");
      emit("closePageBack");
    } else {
      ElMessage.error("保存失败");
    }
  } catch (error) {
    ElMessage.error("保存失败，请重试");
    console.log("error", error);
  }
};
</script>

<style scoped lang="scss">
.save-db-container {
  width: 100%;
  padding: 20px;
  box-sizing: border-box;
  overflow: hidden;
  
  :deep(.el-form) {
    margin: 0;
  }
  
  :deep(.el-form-item) {
    margin-bottom: 16px;
  }
  
  :deep(.el-input) {
    font-size: 14px;
  }
  
  :deep(.el-input-number) {
    width: 100%;
  }
}


.color-display {
  display: flex;
  align-items: center;
  gap: 12px;
  
  .color-block {
    width: 48px;
    height: 24px;
    border-radius: 4px;
    border: 2px solid #dcdfe6;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  }
  
  .color-value {
    font-size: 14px;
    color: #606266;
    font-family: 'Courier New', monospace;
  }
}

.component-hint {
  margin-left: 8px;
  color: #909399;
  font-size: 14px;
}

.button-group {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  margin-top: 16px;
  padding-top: 16px;
  border-top: 1px solid #ebeef5;
  
  :deep(.el-button) {
    margin: 0;
    padding: 10px 20px;
  }
}

:deep(.el-form-item__label) {
  font-weight: 500;
  color: #606266;
  padding-right: 8px;
  width: 80px !important;
}

:deep(.el-input__suffix) {
  color: #909399;
}

:deep(.el-row) {
  margin: 0 -8px;
  
  .el-col {
    padding: 0 8px;
  }
}
</style>
